package com.jigsawapp.movies.ui;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import com.jigsawapp.movies.R;
import com.jigsawapp.movies.ui.fragments.MoviesFragment;
import com.jigsawapp.movies.ui.fragments.TabFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {

    private ViewPager mViewPager;

    private List<Fragment> mTabs = new ArrayList<>();
    private String[] mtitles = new String[] {
            "111",
            "222",
            "333"
    };


    private FragmentPagerAdapter adapter;

    private List<ChangerColorWithText> mTabIndicators = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        ButterKnife.inject(this);
        initView();
        initData();
        initEvent();
//        mViewPager.setOffscreenPageLimit(3);
        mViewPager.setAdapter(adapter);

    }

    private void initEvent() {

        mViewPager.addOnPageChangeListener(this);

    }
    private void initData() {
//        for(String title : mtitles) {
//            TabFragment tabFragment = new TabFragment();
//            Bundle bundle = new Bundle();
//            bundle.putString(TabFragment.TITLE, title);
//            tabFragment.setArguments(bundle);
//            mTabs.add(tabFragment);
//        }

        MoviesFragment bf = new MoviesFragment();
        bf.getMovies("0", "15");
        mTabs.add(bf);

        mTabs.add(new TabFragment());



        TabFragment tabFragment = new TabFragment();
        Bundle bundle = new Bundle();
        bundle.putString(TabFragment.TITLE, mtitles[1]);
        tabFragment.setArguments(bundle);
        mTabs.add(tabFragment);

        adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mTabs.get(position);
            }

            @Override
            public int getCount() {
                return mTabs.size();
            }

            //为了防止滑动viewpager 导致fragment里面的listview 空白
//            @Override
//            public void destroyItem(ViewGroup container, int position, Object object) {
//                //去掉了super
//            }
        };

    }

    private void initView() {
        mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
        ChangerColorWithText one = (ChangerColorWithText) findViewById(R.id.id_one);
        mTabIndicators.add(one);
        ChangerColorWithText two = (ChangerColorWithText) findViewById(R.id.id_two);
        mTabIndicators.add(two);
        ChangerColorWithText three = (ChangerColorWithText) findViewById(R.id.id_three);
        mTabIndicators.add(three);

        one.setOnClickListener(this);
        two.setOnClickListener(this);
        three.setOnClickListener(this);

        one.setIconAlpha(1.0f);
    }


    @Override
    public void onClick(View v) {
        clickTab(v);

    }

    private void clickTab(View v) {
        resetOtherTabs();

        switch (v.getId())
        {
            case R.id.id_one:
                mTabIndicators.get(0).setIconAlpha(1.0f);
                mViewPager.setCurrentItem(0, false);
                break;
            case R.id.id_two:
                mTabIndicators.get(1).setIconAlpha(1.0f);
                mViewPager.setCurrentItem(1, false);
                break;
            case R.id.id_three:
                mTabIndicators.get(2).setIconAlpha(1.0f);
                mViewPager.setCurrentItem(2, false);
                break;

        }
    }

    /**
     * 重置其他的TabIndicator的颜色
     */
    private void resetOtherTabs() {
        for (int i = 0; i < mTabIndicators.size(); i++)
        {
            mTabIndicators.get(i).setIconAlpha(0);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//        Log.i("tt", "asdasd");
        if (positionOffset > 0) {
            ChangerColorWithText left = mTabIndicators.get(position);
            ChangerColorWithText right = mTabIndicators.get(position + 1);
            left.setIconAlpha(1 - positionOffset);
            right.setIconAlpha(positionOffset);
        }
    }

    @Override
    public void onPageSelected(int position) {
    }



    @Override
    public void onPageScrollStateChanged(int state) {

    }


}
